package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.databinding.Bindable;
import android.support.annotation.Nullable;
import android.view.View;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.Locale;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;

/**
 * Created by zxh on 16/11/20.
 */

public class Mine_HomeModel extends ContextObservable {
    private UserInfoModel userInfo;

    @Bindable
    public UserInfoModel getUserInfo() {
        return userInfo;
    }

    public Mine_HomeModel(Context context) {
        super(context);
        userInfo = new UserInfoModel(context);
        load();
    }

    public void onClickCompleteInfo(View view) {
        startNewFragment(R.layout.f_complete_info, BR.userInfo, userInfo);
        new Thread(new Runnable() {
            @Override
            public void run() {
                userInfo.getMoreInfo();
            }
        }).start();
    }

    /**
     * 我的账户
     *
     * @param view
     */
    public void onClickMyAccount(View view) {
        MyAccountModel model = new MyAccountModel(view.getContext());
        startNewFragment(R.layout.f_my_account, BR.account, model);
    }

    /**
     * 我的积分
     *
     * @param view
     */
    public void onClickMyPoints(View view) {
        MyScoreModel model = new MyScoreModel(view.getContext());
        startNewFragment(R.layout.f_my_score, BR.score, model);
    }

    /**
     * 我的消息
     *
     * @param view
     */
    public void onClickMyMessage(View view) {
        MyMessageModel model = new MyMessageModel(view.getContext(), userInfo);
        startNewFragment(R.layout.f_my_message, BR.message, model);
    }

    /**
     * 我购买的视频
     *
     * @param view
     */
    public void onClickMyVideos(View view) {
        MyVideoModel model = new MyVideoModel(view.getContext());
        startNewFragment(R.layout.f_my_video, BR.videos, model);
    }

    /**
     * 我的上传
     *
     * @param view
     */
    public void onClickMyUpload(View view) {
        MyUploadModel model = new MyUploadModel(view.getContext());
        startNewFragment(R.layout.f_my_upload, BR.upload, model);
    }

    /**
     * 我的收藏
     *
     * @param view
     */
    public void onClickMyCollect(View view) {
        MyCollectModel model = new MyCollectModel(view.getContext());
        startNewFragment(R.layout.f_my_collect, BR.collect, model);
    }

    /**
     * 我的悬赏
     *
     * @param view
     */
    public void onClickMyReward(View view) {
        MyRewardModel model = new MyRewardModel(view.getContext());
        startNewFragment(R.layout.f_my_reward, BR.reward, model);
    }

    /**
     * 我的咨询/问题
     *
     * @param view
     */
    public void onClickMyAdvisory(View view) {
        MyAdviceModel model = new MyAdviceModel(view.getContext(), userInfo.isExpertflag());
        startNewFragment(R.layout.f_my_advice, BR.items, model);
    }

    /**
     * 我的草稿箱
     *
     * @param view
     */
    public void onClickMyDraftBox(View view) {
        MyDraftBoxModel model = new MyDraftBoxModel(view.getContext());
        startNewFragment(R.layout.f_my_draft_box, BR.draftBox, model);
    }

    /**
     * 意见反馈
     *
     * @param view
     */
    public void onClickMyFeedback(View view) {
        MyFeedbackModel model = new MyFeedbackModel(view.getContext());
        startNewFragment(R.layout.f_feedback, BR.feedback, model);
    }

    public void onClickFans(View view) {
        MyFansModel model = new MyFansModel(getContext(), User.getCurrentUser().getUid());
        model.setSelf(userInfo);
        startNewFragment(R.layout.f_my_fans, BR.fans, model);
    }

    public void onClickFollow(View view) {
        MyFollowModel model = new MyFollowModel(getContext(), User.getCurrentUser().getUid());
        model.setSelf(userInfo);
        startNewFragment(R.layout.f_my_follow, BR.follows, model);
    }

    public void onClickExit(View view) {
        new AlertDialog.Builder(getContext()).setTitle("提示").setMessage("确定退出吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        HomeActivity.exit(getContext());
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    private void startNewFragment(int layoutId, @Nullable Integer variableId, @Nullable ContextObservable model) {
        CommonFragment fragment = new CommonFragment();
        if (variableId != null) {
            fragment.setVariableId(variableId);
            fragment.setDataModel(model);
        }
        fragment.setLayoutId(layoutId);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    public void load() {
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                UserInfoModel model = WebApi.getUserCenterInfo(getContext());
                if (model != null) {
                    userInfo = model;
                    notifyPropertyChanged(BR.userInfo);
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }
}
